﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Bricks.BaseFramework.GenericForms;
using Bricks.MiuiThemeMaker.BRMiuiThemeMaker;
using Bricks.MiuiThemeMaker.OrmModel;

namespace Bricks.MiuiThemeMaker.MiuiThemeMaker
{
    /*********************************************************
     * 1.版权：积木工场 Bricks Factory
     * 2.作用：元素窗体
     * 3.创建人：罗泽新
     * 4.创建日期：2012-06-28
     * 5.最后修改人：罗泽新
     * 6.最后修改日期：2012-06-28
     * 7.修改项目：
     * *******************************************************/
    public partial class ElementWin : Window
    {
        private ElementBLL bll;
        private ObservableCollection<BF_Element> dataList;
        private List<BF_Element> deletedList;

        public ElementWin()
        {
            InitializeComponent();
            Initialize();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            bll = new ElementBLL();
            dataList = new ObservableCollection<BF_Element>();
            deletedList = new List<BF_Element>();

            LoadElement();  //加载元素数据
        }

        /// <summary>
        /// 加载元素数据
        /// </summary>
        private void LoadElement()
        {
            this.dgElement.ItemsSource = null;
            this.dgElement.Items.Clear();
            bll.GetElementList(SystemData.Instance.DBType).ForEach(v => dataList.Add(v));
            this.dgElement.ItemsSource = dataList;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            BF_Element newItem = new BF_Element();
            newItem.ID = Guid.NewGuid();
            dataList.Add(newItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var selectedItems = this.dgElement.SelectedItems as List<BF_Element>;
            if (selectedItems == null || selectedItems.Count == 0)
                return;
            foreach (var item in selectedItems)
            {
                dataList.Remove(item);
                if (item.ID != Guid.Empty)
                {
                    deletedList.Add(item);
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bll.SaveElementList(dataList, deletedList, SystemData.Instance.DBType);
                MsgBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MsgBox.ShowAlert(ex.Message);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
